package com.qys.livesMall.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.framework.view.BaseQuery;
import com.qys.livesMall.system.mapper.H2AppHomeBannerMapper;
import com.qys.livesMall.system.modal.vo.HomeBannerTypeVO;
import com.qys.livesMall.system.model.dto.sys.H2AppHomeBannerStateDTO;
import com.qys.livesMall.system.model.entity.H2AppHomeBanner;
import com.qys.livesMall.system.model.vo.app.AppHomeBannerVo;
import com.qys.livesMall.system.service.IH2AppHomeBannerService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 首页轮播图 服务实现类
 * </p>
 *
 * @author dp
 * @since 2023-05-02
 */
@Service
public class H2AppHomeBannerServiceImpl extends ServiceImpl<H2AppHomeBannerMapper, H2AppHomeBanner> implements IH2AppHomeBannerService {

    @Override
    public Page<H2AppHomeBanner> page(BaseQuery baseQuery) {
        LambdaQueryWrapper<H2AppHomeBanner> wrapper = Wrappers.lambdaQuery();
        if (null != baseQuery.getStartTime()) {
            wrapper.ge(H2AppHomeBanner::getStartTime, baseQuery.getStartTime());
        }
        if (null != baseQuery.getEndTime()) {
            wrapper.le(H2AppHomeBanner::getEndTime, baseQuery.getEndTime());
        }
        wrapper.orderByDesc(H2AppHomeBanner::getStartTime);

        return baseMapper.selectPage(new Page<>(baseQuery.getPageNum(), baseQuery.getPageSize()), wrapper);
    }

    @Override
    public List<AppHomeBannerVo> listByApp() {
        return baseMapper.listByApp(LocalDateTime.now());
    }

    @Override
    public AppHomeBannerVo queryById(Long id) {
        H2AppHomeBanner appHomeBanner = baseMapper.selectById(id);
        if (null == appHomeBanner) {
            throw new QysException(MessageUtils.message("not.found"));
        }

        AppHomeBannerVo vo = new AppHomeBannerVo();
        BeanUtils.copyProperties(appHomeBanner, vo);
        return vo;
    }

    @Override
    public void updateState(H2AppHomeBannerStateDTO dto) {
        H2AppHomeBanner appHomeBanner = baseMapper.selectById(dto.getId());
        if (null == appHomeBanner) {
            throw new QysException(MessageUtils.message("not.found"));
        }
        appHomeBanner.setState(dto.getState());
        updateById(appHomeBanner);
    }

    @Override
    public Page<HomeBannerTypeVO> typePage(BaseQuery baseQuery) {
        Page<HomeBannerTypeVO> page = new Page<>(baseQuery.getPageNum(), baseQuery.getPageSize());
        List<HomeBannerTypeVO> list=null;
        switch (Integer.valueOf(baseQuery.getId())){
            case 0://商品
                list=baseMapper.getGoodsInfo(page,baseQuery);
                break;
            case 1://主播
                list=baseMapper.getAnchorInfo(page,baseQuery);
                break;
            case 2://店铺
                list=baseMapper.getShopInfo(page,baseQuery);
                break;
            case 3://直播视频
                list=baseMapper.getShortVideoInfo(page,baseQuery);
                break;
            default:
                return null;
        }
        if (CollectionUtils.isNotEmpty(list)){
            page.setRecords(list);
        }
        return page;
    }
}
